<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
?>
    <html>
        <head>
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.pager.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    // call the tablesorter plugin
                    $("table").tablesorter({
                        // sort on the first column and third column, order asc
                        sortList: [[1,0]],
                        headers: {
                            // assign the secound column (we start counting zero)
                            0: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            7: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            8: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            }
                        }
                        ,
                        widthFixed: true, widgets: ['zebra']
                    })
                    .tablesorterPager({container: $("#pager")});
                    ;
                });

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmDrop()
                {
                    var agree=confirm("Are you sure you want to drop selected student?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }

            </script>

            <title>TUP Student Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>

                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="TMMViewProf.php"><b>Professor Management</b></a>&nbsp;&nbsp;
                        <a href="SMMViewStud.php"><b>[ Student Management ]</b></a>&nbsp;&nbsp;
                        <a href="CMMViewCourse.php"><b>Course Management</b></a>&nbsp;&nbsp;
                        <a href="EMSelectProf.php"><b>Enrollment</b></a>
                    </div>

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="EMSelectProf.php" >Professor Select</a>&nbsp;
                        <a href="EMDeselectProf.php" >Remove Assignment</a>&nbsp;
                        <a  href="EMEnrollStud.php" >Enroll Student</a>&nbsp;
                        <a  href="EMSelectSection.php" >Drop Students by Section</a>&nbsp;
                    </div>

                    <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                    </div>
                    <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                        <a class="small" href="RegEditProfile.php"><b>Edit Profile</b></a>
                        <bb>|</bb>
                        <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                        <bb>|</bb></div>
                </div>

                <div align="center" style="  float:left; height:auto; width: 100%;">

                <?php
                $codesecsemyear = $_POST['codesecsemyear'];

                include 'DBLibrary/config.php';
                include 'DBLibrary/openDB.php';

                mysql_select_db($dbname);
                $querytext = "select * from coursetable where codesecsemyear='" . $codesecsemyear . "'";
                $resulttext = mysql_query($querytext);
                $text = mysql_fetch_array($resulttext);


                if (isset($_POST['Drop'])) {
                    $studidno = $_POST['stude'];
                    $totalstud = $_POST['totalstud'];

                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';

                    mysql_select_db($dbname);
                    $dropcourse = "DELETE FROM enrollment WHERE codesecsemyear='" . $codesecsemyear . "' and studidno='" . $studidno . "'";

                    $queryvalue = mysql_query($dropcourse);
                    if ($queryvalue == 0) {
                        echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                        echo "<h3 class=\"err\">Drop failed</h3>";
                    } else if ($queryvalue == 1) {
                        echo "<h3 class=\"err\">Drop successful</h3>";


                        $gettotalstudquery = "select * from coursetable where codesecsemyear='" . $codesecsemyear . "'";
                        $totalstudresult = mysql_fetch_array(mysql_query($gettotalstudquery));

                        $totalstud = $totalstudresult['totalstud'] - 1;
                        $updatequery = "update coursetable set totalstud='" . $totalstud . "' where codesecsemyear='" . $codesecsemyear . "'";
                        mysql_query($updatequery);
                    }
                }
                ?><h2>Students in <?php echo $text['coursecode'] . " " . $text['section'] . " " . $text['semester'] . " SY:" . $text['year']; ?></h2> <?php
//SELECT * FROM enrollment e left join student s on e.studidno=s.studidno;
                include 'DBLibrary/config.php';
                include 'DBLibrary/openDB.php';
                mysql_select_db($dbname);
                $querystud = "SELECT * FROM enrollment e left join student s on e.studidno=s.studidno where codesecsemyear='" . $codesecsemyear . "' order by e.studidno";
                $allstud = mysql_query($querystud);
                $ctr = 0;
                $numrows = mysql_num_rows($allstud);

                echo "<aa>" . $numrows . " total results</aa>";
                ?>
                <table  border="0" id="myTable" class="tablesorter" style="width: 95%;" align="center">
                    <thead><tr>
                            <th>No</th>
                            <th>Student ID</th>
                            <th>Last Name</th>
                            <th>First Name</th>
                            <th>Gender</th>
                            <th>Telephone Number</th>
                            <th>Email Address</th>
                            <th>Drop</th>
                        </tr></thead>
                    <tfoot><tr>
                            <th>No</th>
                            <th>Student ID</th>
                            <th>Last Name</th>
                            <th>First Name</th>
                            <th>Gender</th>
                            <th>Telephone Number</th>
                            <th>Email Address</th>
                            <th>Drop</th>
                        </tr></tfoot>
                    <tbody>

                        <?php
                        while ($row = mysql_fetch_array($allstud)) {
                            $ctr++;
                            //if()
                            //echo "<tr>" . "<td>" . $ctr . "</td>";<th>No.</th>
                            echo "<tr><td>" . $ctr . "</td><td>" . $row['studidno'] . "</td>";
                            echo "<td>" . $row['lname'] . "</td>";
                            echo "<td>" . $row['fname'] . "&nbsp;" . $row['mname'] . ".</td>";
                            echo "<td>" . $row['gender'] . "</td>";
                            echo "<td>" . $row['telno'] . "</td>";
                            echo "<td>" . $row['email'] . "</td>";

                            echo "<form onsubmit=\"return confirmDrop()\" name=\"add\" method=\"post\" action=\"\"><td><input type=\"text\" name=\"stude\" size=\"1\" value=\"" . $row['studidno'] . "\" style=\"visibility:hidden;\"/>
            <input type=\"submit\" name=\"Drop\" value=\"Drop\"/><input type=\"text\" name=\"codesecsemyear\" size=\"1\" value=\"" . $codesecsemyear . "\" style=\"visibility:hidden;\"/>
            </td>\n";
                            echo "</form>";
                        }
                        include 'DBLibrary/closeDB.php';
                        ?> </tbody>
                </table>

                <div id="pager" class="pager" align="center" style="float:left; height: auto; width: 100%;">
                    <form>
                        <img src="addons/pager/icons/first.png" class="first"/>
                        <img src="addons/pager/icons/prev.png" class="prev"/>
                        <input readonly="readonly" type="text" class="pagedisplay"/>
                        <img src="addons/pager/icons/next.png" class="next"/>
                        <img src="addons/pager/icons/last.png" class="last"/>
                        <select class="pagesize">
                            <option selected="selected"  value="10">10</option>

                            <option value="20">20</option>
                            <option value="30">30</option>
                            <option  value="40">40</option>
                        </select>
                    </form>
                    <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
                        <div align="center" style=" height: 4px; width: auto;"></div>
                        <bb class ="small">Copyright &#169; 2011</bb>
                    </div>
                </div>
                <br>


            </div>
        </div>

    </body>
</html>
<?php
                    } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
                        echo "<script language='javascript'>window.location='UMMViewUser.php'</script>";
                    } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
                        echo "<script language='javascript'>window.location='AMMSelectSection.php'</script>";
                    }
?>